package com.aiju.cash.ui.signbank.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.enter.util.EnterHelper;
import com.aiju.cash.ui.signbank.bean.PersonInfo;
import com.aiju.cash.util.ImageDialogUtil;
import com.aiju.common.Verify.EmptyVerify;
import com.aiju.common.Verify.VerifyManger;
import com.aiju.forexpand.addresspicker.SinglePicker;
import com.aiju.forexpand.datepicker.DatePicker;
import com.aiju.utils.DateUtil;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemRadioButton;
import com.aiju.widgit.layout.SettingItemTextView;

/**
 * @author: 流冥
 * @date: 2017-07-13
 * @Description: 法人信息提交
 */
public class PersonInfoCommitActivity extends BaseTitleActivity implements View.OnClickListener {
    private SettingItemEditView name;
    private SettingItemRadioButton sex;
    private SettingItemTextView area;
    private SettingItemTextView type;
    private SettingItemTextView effect;
    private SettingItemTextView expire;
    private SettingItemEditView identify;
    private ImageView identifyPositive;
    private ImageView identifynegative;
    PersonInfo personInfo = new PersonInfo();
    private int REQUEST_CODE_1 = 1;
    private int REQUEST_CODE_2 = 2;
    private String sexTag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info_commit);
        initView();
    }

    void initView() {
        mAiJuTitleBarView.setCenterTitle("法人信息提交");
        mAiJuTitleBarView.setRightTitle("提交");
        name = (SettingItemEditView) findViewById(R.id.name);
        identify = (SettingItemEditView) findViewById(R.id.code);
        sex = (SettingItemRadioButton) findViewById(R.id.sex);
        area = (SettingItemTextView) findViewById(R.id.area);
        effect = (SettingItemTextView) findViewById(R.id.effect_time);
        expire = (SettingItemTextView) findViewById(R.id.expire_time);
        type = (SettingItemTextView) findViewById(R.id.type);
        identifyPositive = (ImageView) findViewById(R.id.identity_card_positive);
        identifynegative = (ImageView) findViewById(R.id.identity_card_negative);
        EditText nameRightText = (EditText) name.getRightView();
        nameRightText.setHint("请填写");
        EditText identifyRightText = (EditText) identify.getRightView();
        identifyRightText.setHint("请填写");
        name.setEditable(true);
        identify.setEditable(true);
        area.setOnClickListener(this);
        type.setOnClickListener(this);
        effect.setOnClickListener(this);
        expire.setOnClickListener(this);
        identifyPositive.setOnClickListener(this);
        identifynegative.setOnClickListener(this);

        sex.setChangeListener(new SettingItemRadioButton.OnChangeListener() {
            @Override
            public void change(String item) {
                personInfo.setSex(item);
                sexTag = item;
            }
        });
        name.getRightView().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                personInfo.setName(s.toString().trim());
            }
        });
        identify.getRightView().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                personInfo.setCode(s.toString().trim());
            }
        });
        mAiJuTitleBarView.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                personInfo.toString();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.area:
                showArea("请选择法人证件所属地区");
                break;
            case R.id.type:
                showtype("请选择法人证件类型");
                break;
            case R.id.effect_time:
                showEffectTime(effect.getRightView(), "法人证件生效时间");
                break;
            case R.id.expire_time:
                showExpireTime(expire.getRightView(), "法人证件过期时间");
                break;
            case R.id.identity_card_positive:
                ImageDialogUtil.showSelectPicDialogByRequetCode(PersonInfoCommitActivity.this, REQUEST_CODE_1);
                break;
            case R.id.identity_card_negative:
                ImageDialogUtil.showSelectPicDialogByRequetCode(PersonInfoCommitActivity.this, REQUEST_CODE_2);
                break;
        }

    }

    void showArea(String title) {
        SinglePicker dataPicker = new SinglePicker();
        dataPicker.selectAddressDialog(this, area.getRightView(), R.array.area);
        dataPicker.getTitleTextView().setText(title);
        dataPicker.setChangeCity(new SinglePicker.OnChangeCity() {
            @Override
            public void onChangeCity(String province) {
                personInfo.setArea(province);
            }
        });
    }

    void showtype(String title) {
        SinglePicker dataPicker = new SinglePicker();
        dataPicker.selectAddressDialog(this, type.getRightView(), R.array.type);
        dataPicker.getTitleTextView().setText(title);
        dataPicker.setChangeCity(new SinglePicker.OnChangeCity() {
            @Override
            public void onChangeCity(String province) {
                personInfo.setType(province);
            }
        });
    }

    /**
     * 生效时间
     */
    private void showEffectTime(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectDateDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        dataPicker.getTitle().setText(title);
        dataPicker.setChangeDate(new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                personInfo.setEffect(date);
            }
        });
    }

    /**
     * 过期时间
     */
    private void showExpireTime(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectDateDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        dataPicker.getTitle().setText(title);
        dataPicker.setChangeDate(new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                personInfo.setExpire(date);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == REQUEST_CODE_1) {
                personInfo.setIdentifyPositive(EnterHelper.onActivityResultByRequestCode(identifyPositive, resultCode, data));
            } else if (requestCode == REQUEST_CODE_2) {
                personInfo.setIdentifyNegative(EnterHelper.onActivityResultByRequestCode(identifynegative, resultCode, data));
            }
        }
    }

    private boolean check() {
        return new VerifyManger()
                .addVerify(new EmptyVerify(name.getRightText(), "请填写法人姓名"))
                .addVerify(new EmptyVerify(sexTag, "请填写法人性别"))
                .addVerify(new EmptyVerify(check(area.getRightString(), "请选择"), "请填写法人证件所属区域"))
                .addVerify(new EmptyVerify(check(name.getRightString(), "请选择"), "请填写法人姓名"))
                .addVerify(new EmptyVerify(check(effect.getRightString(), "请选择"), "请填写法人姓名"))
                .addVerify(new EmptyVerify(check(expire.getRightString(), "请选择"), "请填写法人姓名"))
                .execute();

    }

    private String check(String text, String standerd) {
        return standerd.equals(text) ? "" : text;
    }
}
